package java8.test.exercise;

import java8.test.data.Person;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

//集合排序
public class Learning01 {

    /**
     * 需求：已知一个ArrayList中有若干个Person对象，
     * 将这些Persion对象按照年龄进行降序排序
     */
    public static void test01(){
        List<Person> list = new ArrayList<>();
        list.add(new Person("a", 10));
        list.add(new Person("b", 9));
        list.add(new Person("c", 8));
        list.add(new Person("d", 11));
        list.sort((o1, o2) -> {
            return o2.getAge() - o1.getAge();
        });
        System.out.println(list);
    }

    public static void test02(){
        List<Integer> list = new ArrayList<>();
        list.add(10);
        list.add(9);
        list.add(8);
        list.add(11);
        list.sort((o1, o2) -> {
            return o1 - o2;
        });
        System.out.println(list);
    }

    /**
     * 需求：已知一个ArrayList中有若干个Person对象，
     * 将这些Persion对象按照年龄进行降序排序
     */
    public static void test03(){
        List<Person> list = new ArrayList<>();
        list.add(new Person("a", 10));
        list.add(new Person("b", 9));
        list.add(new Person("c", 8));
        list.add(new Person("d", 11));
        Collections.sort(list, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return (int)o2- (int)o1;
            }
        });
        System.out.println(list);
    }

    public static void main(String[] args) {
        test03();
    }

}
